package com.flycent.fanbao.net.request;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;

import com.flycent.fanbao.bean.Product;
import com.flycent.fanbao.net.API;
import com.flycent.fanbao.net.BaseResponseHandler;
import com.flycent.fanbao.net.HttpRequestClient;
import com.flycent.fanbao.net.response.BaseStatusResponse;
import com.flycent.fanbao.net.response.CheckNewVersionResponse;
import com.flycent.fanbao.net.response.HotSearchListResponse;
import com.flycent.fanbao.net.response.ProductDetailResponse;
import com.flycent.fanbao.net.response.ProductSeriesDownloadResponse;
import com.flycent.fanbao.net.response.SearchProductResponse;
import com.flycent.fanbao.util.PackageInfoUtil;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;

/**
 * 所有的网络请求
 * 
 * @author Liu.Wenzhu <lwz0316.gmail.com>
 * @date 2014年7月7日 下午1:51:15
 * 
 */
public class Request {
	
	/**
	 * 创建符合要求的请求参数对象
	 * @return 返回的对象中包括 key, timestamp, token 等每个请求都必要的字段
	 */
	public static RequestParams createRequestParams() {
		return createRequestParams((Map<String, String>)null);
	}
	
	/**
	 * 创建符合要求的请求参数对象
	 * @return 返回的对象中包括 key, timestamp, token 等每个请求都必要的字段
	 */
	public static RequestParams createRequestParams(Map<String, String> data) {
		RequestParams params = new RequestParams(data);
		long timestamp = System.currentTimeMillis();
//		params.put("mobileType", "2");
//		params.put("timestamp", Long.toString(timestamp));
//		params.put("key", SecretUtil.createSecretKey(timestamp));
//		params.put("token", UserManager.restoreUserToken());
//		params.put("country", LocaleManager.INSTANCE.getCurrentLocale().toString());
		return params;
	}
	
	public static RequestParams createRequestParams(String timestampKey) {
		Map<String, String> params = new HashMap<String, String>();
//		params.put("updateTime", TimestampManager.getTimestamp(timestampKey));
		return createRequestParams(params);
	}
	
	/**
	 * 注册 url 为静默请求（没有 loading 效果）
	 * @param url
	 */
	public static void registerSilentRequest(String url) {
		HttpRequestClient.registerUnshowDialogAPIFilter.add(url);
	} 
	
	@SuppressWarnings("unused")
	private static boolean isLogin(Context context) {
		if( context instanceof Activity ) {
//			return BaseActivity.isLogin((Activity) context );
		}
    	return true;
    }

	
	/**
	 * 查询搜索列表
	 */
	public static void queryHotSearchList(Context context,
			BaseResponseHandler<HotSearchListResponse> responseHandler) {
		RequestParams params = createRequestParams();
		HttpRequestClient.post(context, API.QUERY_HOT_SEARCH_LIST, params, responseHandler);
	}
	
	
	public static void queryProduct(Context context,BaseResponseHandler<Product[]> responseHandler){
		RequestParams params = new RequestParams();
		params.put("quantity", "3");
		HttpRequestClient.get(context,"http://192.168.1.109:61088/hsc/rest/home_page/group_buying", params, responseHandler);
	}
	
	
	public static void searchProduct(Context context, String keyword, BaseResponseHandler<SearchProductResponse> responseHandler) {
		RequestParams params = createRequestParams();
		params.put("keyword", keyword);
		HttpRequestClient.post(context, API.SEARCH_PRODUCT, params, responseHandler);
	}
	
	public static void checkNewVersion(Context context, BaseResponseHandler<CheckNewVersionResponse> responseHandler) {
		RequestParams params = createRequestParams();
		params.put("version", PackageInfoUtil.getVersionName(context));
		HttpRequestClient.post(context, API.CHECK_NEW_VERSION, params, responseHandler);
	}
	
	
	
}
